package com.iotboss.web.controller.iot;

import com.alibaba.fastjson.JSON;
import com.iotboss.common.annotation.Log;
import com.iotboss.common.core.controller.BaseController;
import com.iotboss.common.core.domain.AjaxResult;
import com.iotboss.common.core.domain.entity.SysUser;
import com.iotboss.common.core.domain.entity.iot.device;
import com.iotboss.common.core.domain.model.LoginUser;
import com.iotboss.common.enums.BusinessType;
import com.iotboss.common.utils.AesEncryptUtil;
import com.iotboss.common.utils.SecurityUtils;
import com.iotboss.common.utils.ServletUtils;
import com.iotboss.common.utils.ip.IpUtils;
import com.iotboss.common.utils.poi.ExcelUtil;
import com.iotboss.common.utils.spring.SpringUtils;
import com.iotboss.system.service.*;
import com.iotboss.web.core.config.MyBaseController;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iotboss.framework.web.service.TokenService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

/**
 * 用户信息
 *
 * @author iotboss
 */
@RestController
@RequestMapping("/device")
public class IotDeviceController extends MyBaseController
{

    @Resource
    IIotDeviceService iIotDeviceService;//设备service层

    /**
     * 下载模板
     * @param response
     */

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<device> util = new ExcelUtil<device>(device.class);
        util.importTemplateExcel(response, "设备数据");
    }

    /**
     * 设备导入
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "设备管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        iIotDeviceService.importDeviceByExcl(file,user);


        String message = "导入成功 请前往操作记录-设备操作记录查看结果";
        return success(message);
    }

    @Log(title = "设备管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('iotboss:device:list')")
    @PostMapping("/list")
    public String getList(@RequestBody String Pstr) throws Exception
    {
        HashMap<String, Object> Parammap = new HashMap<String, Object>();
        if (Pstr != null) {
            Pstr = Pstr.replace("%2F", "/");//转义 /
        }
        try {
            Pstr = AesEncryptUtil.desEncrypt(Pstr);
            Parammap.putAll(JSON.parseObject((String) Pstr));//参数解析 parammap
            SysUser user = SecurityUtils.getLoginUser().getUser();
            Parammap.put("dept_id",user.getDeptId());//放入deptId


            return MyRetunSuccess(iIotDeviceService.getList(Parammap),"无返回");
        } catch (Exception e) {
            String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
            logger.error("<br/> yunze:tariffPlan:add  <br/> Pstr = " + Pstr + " <br/> ip =  " + ip + " <br/> ", e.toString());
        }
        return Myerr("设备列表获取 异常！");
    }
}
